package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/mq")
public class MqController {

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 延迟消息-插件
     */
    @GetMapping("/sendDelay")
    public Result sendDelay() {
        //创建时间格式化类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //显示时间
        System.out.println("消息发送时间：\t" + simpleDateFormat.format(new Date()));

        rabbitService.sendDelayedMessage(
                DelayedMqConfig.exchange_delay,
                DelayedMqConfig.routing_delay,
                "延迟插件！",
                10);
//        rabbitTemplate.convertAndSend(
//                DelayedMqConfig.exchange_delay,
//                DelayedMqConfig.routing_delay,
//                "延迟插件！",message->{
//                    //显示时间
//                    System.out.println("消息发送时间：\t"+simpleDateFormat.format(new Date()));
//                    message.getMessageProperties().setDelay(10*1000);
//
//                    return message;
//                });


        return Result.ok();
    }


    /**
     * 延迟消息-死信
     *
     * @return
     */
    @GetMapping("/sendDeadLetter")
    public Result sendDeadLetter() {

        //创建时间格式化类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //显示时间
        System.out.println("消息发送时间：\t" + simpleDateFormat.format(new Date()));
        //发送消息
        rabbitTemplate.convertAndSend(
                DeadLetterMqConfig.exchange_dead,
                DeadLetterMqConfig.routing_dead_1,
                "OK"
        );

        return Result.ok();
    }


    @GetMapping("/sendConfirm")
    public Result sendConfirm() {

        rabbitService.sendMessage("exchange.confirm", "routin11g.confirm",
                "来人了，开始接客吧!");


        return Result.ok();
    }


}
